/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package com.codeplex.smartrctx.configurator.main;

import java.util.Scanner;
import java.util.regex.MatchResult;

import com.codeplex.smartrctx.configurator.communication.CommLink.DataReceivedHandler;

import rcremote.main.R;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class LogViewFragment extends Fragment implements DataReceivedHandler {
	private class Updater implements Runnable {
		private String text;
		private TextView target;
		private boolean append;

		public Updater(TextView target, String text, boolean append) {
			this.text = text;
			this.target = target;
			this.append = append;
		}

		@Override
		public void run() {
			if (this.append) {
				target.append(text + "\n");
			} else {
				target.setText(text);
			}
//			int scroll_amount = target.getBottom();
			 int scroll_amount = (int) (target.getLineCount() * target.getLineHeight()) - (target.getBottom() - target.getTop());
			    
		    target.scrollTo(0, scroll_amount);
		}
	}

	private TextView chView[] = new TextView[6];
	private TextView logView;
	private Handler handler = new Handler();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.log, container);

		chView[0] = (TextView) view.findViewById(R.id.ch1Display);
		chView[1] = (TextView) view.findViewById(R.id.ch2Display);
		chView[2] = (TextView) view.findViewById(R.id.ch3Display);
		chView[3] = (TextView) view.findViewById(R.id.ch4Display);
		chView[4] = (TextView) view.findViewById(R.id.ch5Display);
		chView[5] = (TextView) view.findViewById(R.id.ch6Display);

		logView = (TextView) view.findViewById(R.id.commLog);
		logView.setMovementMethod(new ScrollingMovementMethod());

		return view;
	}

	public void clearLog() {
		this.logView.setText("");
	}
	
	@Override
	public void dataReceived(String data) {
		if (data.contains("Channels:")) {
			Scanner scanner = new Scanner(data);
			scanner.findInLine("Channels: ([-0-9]+); ([-0-9]+); ([-0-9]+); ([-0-9]+); ([-0-9]+); ([-0-9]+);");

			try {
				MatchResult result = scanner.match();

				for (int i = 1; i <= result.groupCount(); ++i) {
					Integer t = Integer.parseInt(result.group(i));
					t = t & 0x3ff;
					t -= 0x200;
					this.handler.post(new Updater(chView[i - 1], t.toString(), false));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			scanner.close();
		} else {
			this.handler.post(new Updater(logView, data, true));
		}
	}
}
